/**
 * 时间工具类
 */

/**
 * 获取某年从某月到本月或从本月到某月的累计天数
 * @param year 当前年份
 * @param month 月份范围
 * @param type 0:求当前月, 1:求前几月  2:求后几月
 * @param currentMonth 当前月份
 */
export function getDayByYearAndMonthRang(year: number, month: number, type: number = 0, currentMonth: number) {
  // 总天数
  let day = 0
  // 总共需要循环的次数
  let size = 0
  // 求本月
  if (type == 0) {
    size = 1
  } else if (type == 1) {
    // 求前几个月
    size = currentMonth - (currentMonth - month)
    if (month != 0) {
      size--
    }
  } else {
    //求后几个月
    size = currentMonth + (month - currentMonth)
  }
  // 从当前月开始计算
  for (let index = 0; index <= size; index++) {
    // 本月不需要考虑任何情况
    switch (currentMonth) {
      case 1:
        day += 31
        break
      case 2:
        day += year % 4 == 0 && year % 100 != 0 ? 29 : 28
        break
      case 3:
        day += 31
        break
      case 4:
        day += 30
        break
      case 5:
        day += 31
        break
      case 6:
        day += 30
        break
      case 7:
        day += 31
        break
      case 8:
        day += 31
        break
      case 9:
        day += 30
        break
      case 10:
        day += 31
        break
      case 11:
        day += 30
        break
      case 12:
        day += 31
        break
      default:
        throw reportError('月份有误')
    }
    // 求前几个月边界情况
    if (type == 1) {
      currentMonth--
      if (currentMonth == 0) {
        currentMonth = 12
        year--
      }
    }
    // 求后几个月边界情况
    if (type == 2) {
      currentMonth++
      if (currentMonth == 13) {
        currentMonth = 1
        year++
      }
    }
  }
  return day
}

/**
 * 是否是平年
 * @param year 年份
 */
export function isCommonYear(year: number): boolean {
  return !((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
}

/**
 * 获取某年某月有多少天
 * @param year 年份
 * @param month 月份
 */
export function getDayByYearAndMonth(year: number, month: number): number {
  switch (month) {
    case 1:
      return 31
      break
    case 2:
      return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28
      break
    case 3:
      return 31
      break
    case 4:
      return 30
      break
    case 5:
      return 31
      break
    case 6:
      return 30
      break
    case 7:
      return 31
      break
    case 8:
      return 31
      break
    case 9:
      return 30
      break
    case 10:
      return 31
      break
    case 11:
      return 30
      break
    case 12:
      return 31
      break
    default:
      throw new Error('get DayByYearAndMonth error!')
      break
  }
}

/**
 * 获取当前年月日
 */
export function getCurrentYMD() {
  const date = new Date()
  // 获取年数
  const year = date.getFullYear()
  // 获取月数
  const month = date.getMonth() + 1
  // 获取天数
  const day = date.getDate()
  return {
    year: year,
    month: month,
    day: day,
  }
}

/**
 * 获取前几月和后几月的日期（毫秒显示)
 * @param previousMonth 前几个月
 * @param afterMonth 后几个月,0为当月
 */
export function getRangByCurrentDate(previousMonth = 1, afterMonth = 0) {
  const time = getCurrentYMD()
  //本月已经过去的时间
  const dayTime = 3600 * 1000 * 24 * time.day
  // 前几个月的时间 = 当前时间 - 前几月的时间 + 本月已经过去的时间
  const monthBefore = new Date()
  monthBefore.setTime(
    monthBefore.getTime() -
      (3600 * 1000 * 24 * getDayByYearAndMonthRang(time.year, previousMonth, 1, time.month) + dayTime),
  )
  // 后几个月的时间 当前时间 + 后几月的时间 - 本月已经过去的时间
  const monthAfter = new Date()
  monthAfter.setTime(
    monthAfter.getTime() + 3600 * 1000 * 24 * getDayByYearAndMonthRang(time.year, afterMonth, 2, time.month) - dayTime,
  )
  return {
    startTime: monthBefore,
    stopTime: monthAfter,
  }
}

export interface LocalDate {
  year: number
  month: number
  day: number
  hour: number
  minute: number
  second: number
  week: string
  mete: Date
}

/**
 * 获取本地日期
 * @param date 不传为当前时间
 */
export function getLocalDate(date: Date = new Date()): LocalDate {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const week = date.getDay()
  const arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  return <LocalDate>{
    year,
    month,
    day,
    hour,
    minute,
    second,
    week: arr[week],
    mete: date,
  }
}

/**
 * 获取当前全日期(格式 2020-03-06 01:01:01)
 */
export function fullDate(): string {
  const { year, month, day, hour, minute, second, week } = getLocalDate()
  return `${year}-${NumToStrFullZero(month)}-${NumToStrFullZero(day)} ${NumToStrFullZero(hour)}:${NumToStrFullZero(
    minute,
  )}:${NumToStrFullZero(second)} ${week}`
}

/**
 * 数值转字符串，小于10补0
 * @param val
 * @constructor
 */
export function NumToStrFullZero(val: number): string {
  return val > 9 ? val + '' : '0' + val
}
